﻿using System;
using System.Collections.Generic;
using System.Text;
using MbUnit.Framework;
using MyTestRunner.Infrastructure;
using System.Reflection;

namespace MyTestRunner.Tests.Infrastructure
{
    /// <summary>
    /// Specifies how the finding of methods with a given attribute should behave.
    /// </summary>
    [TestFixture]
    [TestsOn(typeof(ReflectionHelper))]
    public class WhenFindingPublicMethodsWithAttribute
    {
        /// <summary>
        /// Should throw when the assembly argument is null.
        /// </summary>
        [Test]
        [ExpectedArgumentNullException]
        public void ShouldThrowGivenAssemblyIsNull()
        {
            Assembly assembly = null;
            string attributeName = typeof(TestAttribute).Name;

            IEnumerable<MethodInfo> foundMethods =ReflectionHelper.FindPublicMethodsWithAttribute(assembly, attributeName);
            Assert.IsFalse(EnumerableHelper.HasAtleastOneElement<MethodInfo>(foundMethods));
        }

        /// <summary>
        /// Should throw when the given attribute name is null or empty.
        /// </summary>
        [Test]
        [ExpectedArgumentException]
        public void ShouldThrowGivenAttributeNameIsNull()
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            string attributeName = null;

            IEnumerable<MethodInfo> foundMethods = ReflectionHelper.FindPublicMethodsWithAttribute(assembly, attributeName);
            Assert.IsFalse(EnumerableHelper.HasAtleastOneElement<MethodInfo>(foundMethods));
        }

        /// <summary>
        /// Should be able to discover methods with the given attribute.
        /// </summary>
        [Test]
        public void ShouldBeAbleToDiscoverMethods()
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            string attributeName = typeof(TestAttribute).FullName;

            IEnumerable<MethodInfo> foundMethods = ReflectionHelper.FindPublicMethodsWithAttribute(assembly, attributeName);
            Assert.IsTrue(EnumerableHelper.HasAtleastOneElement<MethodInfo>(foundMethods));
        }
    }
}
